
const apiUrl = process.env.VUE_APP_BRACELET_API
const baseRequest = (url, method, params, contentType) => {
    console.log(uni.getStorageSync('access_token'))
    const token = uni.getStorageSync('access_token')

    return new Promise((resolve, reject) => {
        uni.request({
            url: apiUrl + url, // 测试接口
            method: method,
            data: params,
            header: {
                'content-type': contentType || 'application/json',
                // 'access_token': token,
                // 'Authorization':'Bearer ' +`'`+ token +`'`,
                'Authorization': 'Bearer ' + token
            }
        }).then((response) => {
            console.log(response, 'response')
            const res = response

            if (res.statusCode === 401 || res.data.code === -1) {
                // 接口出问题，可能是token失效了，返回登陆页
                // uni.clearStorage()
                uni.navigateTo({
                    url: '/pages/login/loginSelect'
                })
            } else {
                resolve(res.data)
            }
        }).catch(error => {
            // 弹出消息提示
            uni.showToast({
                icon: 'none', // 不显示图标，最多显示2行
                title: '服务器错误！',
                duration: 2000
            })

            const err = error[0]

            reject(err)
        })
    })
}

export default {
    baseRequest
}
